package com.chen.palmar.common.widget.dialog;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.chen.palmar.R;

import me.shaohui.bottomdialog.BaseBottomDialog;


/**
 * Created by Chen on 2017/4/27.
 * 功能描述：
 */

public class EditTextDialog extends BaseBottomDialog {

    private EditText mEditText;
    private TextView tvTitle;
    private String title;
    private View.OnClickListener mListener;

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_approval;
    }

    @Override
    public void bindView(View v) {
        mEditText = (EditText) v.findViewById(R.id.et_dialog_advice);
        mEditText.post(() -> {
            InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(mEditText, 0);
        });
        Button btnSure = (Button) v.findViewById(R.id.btn_dialog_sure);
        v.findViewById(R.id.btn_dialog_cancle).setOnClickListener(view -> EditTextDialog.this.dismiss());
        btnSure.setOnClickListener(mListener);
        tvTitle = (TextView) v.findViewById(R.id.tv_dialog_title);
        if (title == null) {
            title = "发表评论";
        } else {
            title += "发表评论";
        }
        tvTitle.setText(title);
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s)) {
                    btnSure.setTextColor(Color.parseColor("#b0b0b0"));
                } else {
                    btnSure.setTextColor(Color.parseColor("#333333"));
                }
            }
        });
    }

    public void setListener(View.OnClickListener listener) {
        this.mListener = listener;
    }

    public void setTitle(String str) {
        this.title = str;
    }

    public String getText() {
        return mEditText.getText().toString();
    }

    @Override
    public float getDimAmount() {
        return 0.5f;
    }

    @Override
    public void onResume() {
        super.onResume();
    }
}
